export const increase = ()=>{
    return {
        type:'INCREASE'
    }
}

export const decrease = ()=>{
    return {
        type:'DECREASE'
    }
}

export const getListData = (data)=>{
    return {
        type:"GETLISTS",
        data
    }
}
export const addOne= (data)=>{
    return{
        type:"Add",
        data
    }
}


export function fetchList(){
    return dispatch=>{
        var url = 'api/v2/movie/top250';
        return fetch(url)
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            dispatch(getListData(data))
        })
    }
}
//关于Cate
export const cateList = (data)=>{
    return {
        type:"CATE_LIST_GET",
        data
    }
}
export function getCateList(page){
    return dispatch=>{
        var url = 'http://localhost:3000/cate/list';
        return fetch(url)
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            console.log("cate");
            console.log(data);
            dispatch(cateList(data))
        })
    }
}
    // getCateGoods
export const selGoods = (data)=>{
    return {
        type:"SEL_LIST_GOODS",
        data
    }
}
export function selCateGoods(data){
    var json ={};
    if(data!="all"){
        json={
            id:data
        }
    }
    return dispatch=>{
        var url = 'http://localhost:3000/cate/listss';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            // console.log(data.toString());
            // selCateGoodsRes(data.toString())
            fetch("http://localhost:3000/goods/listss",{
                method:"post",
                body:JSON.stringify({"ids":data.toString()}),
                headers:{
                    'Accept': 'application/json',
                    'Content-Type': 'application/json' 
                }
            })
            .then(res=>{
                return (res.json())
            })
            .then(data=>{
                // console.log(data);
                dispatch(selGoods(data))
            })
        })
    }
}
 //=-====------------------------------
export const goodsList = (data)=>{
    return {
        type:"GOODS_LIST_POST",
        data
    }
}
export function postGoodList(page){
    console.log("page");
    console.log(page);
    var json = {};
    if(page){
        json={
            "limit":12,
            page:page
        }
    }else{
        json={
            "limit":12,
            page:1
        }
    }
    return dispatch=>{
        var url = 'http://localhost:3000/goods/list';
        // console.log(url);
        return fetch(url,{
            method:"post",
            // mode: "no-cors",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            dispatch(goodsList(data))
        })
    }
}

export const findGoodsDPT = (data)=>{
    return {
        type:"GOODS_LIST_POST_FIND",
        data
    }
}
export function findGoods(keyword){
    var json = {
        title:keyword,
        limit:12
    }
    return dispatch=>{
        var url = 'http://localhost:3000/goods/list';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            dispatch(findGoodsDPT(data))
        })
    }
}

//findGoods
//关于News--------------------------------------

export const newsListDPT = (data)=>{
    return {
        type:"POST_NEWS_LIST",
        data
    }
}
export function postNewsList(page){
    var json = {
        page:1,
        limit:12
    }
    if(page){
        json.page = Number(page);
    }
    return dispatch=>{
        var url = 'http://localhost:3000/news/list';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            
            dispatch(newsListDPT(data))
        })
    }
}



//forTop
export const newsListDPT_top = (data)=>{
    return {
        type:"POST_NEWS_LIST_TOP",
        data
    }
}
export function postNewsList_top(page){
    var json = {
        page:1,
        limit:12
    }
    if(page){
        json.page = Number(page);
    }
    return dispatch=>{
        var url = 'http://localhost:3000/news/list';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            
            dispatch(newsListDPT_top(data))
        })
    }
}
//selectNews

export const selectNewsDPT = (data)=>{
    return {
        type:"SEL_LIST_NEWS",
        data
    }
}
export function selectNews(data){
    var json ={};
    if(data!="all"){
        json={
            id:data
        }
    }
    return dispatch=>{
        var url = 'http://localhost:3000/cate/listss';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            fetch("http://localhost:3000/news/listss",{
                method:"post",
                body:JSON.stringify({"ids":data.toString()}),
                headers:{
                    'Accept': 'application/json',
                    'Content-Type': 'application/json' 
                }
            })
            .then(res=>{
                return (res.json())
            })
            .then(data=>{
                dispatch(selectNewsDPT(data))
            })
        })
    }
}


//about 购物车 AddCar

// export const AddCarDPT = (data)=>{
//     return {
//         type:"ADD_CAR",
//         data
//     }
// }
export const AddCar = (data)=>{
    return {
        type:'ADDPRODUCTTOCAR',
        data:JSON.parse(data)
    }
}

// export function AddCar(str){
//     return dispatch=>{
//         dispatch(AddCarDPT({
//             data:JSON.parse(str)
//         }))
//     }
      
// }

//about comments

//fetchComments
export const fetchCommentsDPT = (data)=>{
    return {
        type:"POST_COMMENT_LIST",
        data
    }
}
export function fetchComments(param){
    var json={
        belongid:param
    }
    console.log(json)
    return dispatch=>{
        var url = 'http://localhost:3000/comment/list';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            dispatch(fetchCommentsDPT(data))
        })
    }
}
//subComment
export const subCommentDPT = (data)=>{
    return {
        type:"SUB_COMMENT",
        data
    }
}
export function subComment(json){
    return dispatch=>{
        var url = 'http://localhost:3000/comment/data';
        return fetch(url,{
            method:"post",
            body:JSON.stringify(json),
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
            
        })
        .then(res=>{
            return res.json()
        })
        .then(data=>{
            fetch("http://localhost:3000/comment/list",{
                method:"post",
                body:JSON.stringify({"belongid":data.belongid}),
                headers:{
                    'Accept': 'application/json',
                    'Content-Type': 'application/json' 
                }
            })
            .then(res=>{
                return (res.json())
            })
            .then(data=>{
                dispatch(subCommentDPT(data))
            })
        })
    }
}


